package com.example.coursedesign;

import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.coursedesign.dao.UserDao;
import com.example.coursedesign.entity.User;

/**
 * function：连接注册页面
 */
public class register extends AppCompatActivity {
    private static final String TAG = "mysql-coursedesign-register";
    EditText userAccount = null;
    EditText userPassword = null;
    EditText userName = null;
    EditText passwordConfirm = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        // 设置标题栏的标题
        getSupportActionBar().setTitle(" ");

        userAccount = findViewById(R.id.userAccount);
        userPassword = findViewById(R.id.userPassword);
        userName = findViewById(R.id.userName);
        passwordConfirm = findViewById(R.id.pwdConfirm);
    }

    public void log(View view) {
        startActivity(new Intent(getApplicationContext(), login.class));
    }

    public void register(View view) {
        String userAccount1 = userAccount.getText().toString().trim();
        String userPassword1 = userPassword.getText().toString().trim();
        String userName1 = userName.getText().toString().trim();
        String passwordConfirm1 = passwordConfirm.getText().toString().trim();

        if (userAccount1.isEmpty()&&userPassword1.isEmpty()&&userName1.isEmpty()&&passwordConfirm1.isEmpty()) {
            Toast.makeText(getApplicationContext(), "请输入用户信息！", Toast.LENGTH_SHORT).show();
            return;
        }
        if (userAccount1.isEmpty()) {
            Toast.makeText(getApplicationContext(), "账号不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }

        if (userPassword1.isEmpty()) {
            Toast.makeText(getApplicationContext(), "密码不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }

        if (userName1.isEmpty()) {
            Toast.makeText(getApplicationContext(), "昵称不能为空！", Toast.LENGTH_SHORT).show();
            return;
        }

        if (passwordConfirm1.isEmpty()) {
            Toast.makeText(getApplicationContext(), "请再次输入密码！", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!userPassword1.equals(passwordConfirm1)) {
            Toast.makeText(getApplicationContext(), "两次密码输入不一致！", Toast.LENGTH_SHORT).show();
            return;
        }

        User user = new User();
        user.setAccount(userAccount1);
        user.setName(userName1);
        user.setPassword(userPassword1);
        user.setGender("女");
        user.setRole("USER");
        user.setAvatar("hsudf");
        user.setPhone("1122334455");
       // user.setcreate_by("ADMIN");

        new Thread() {
            @Override
            public void run() {
                int msg = 0;
                UserDao userDao = new UserDao();
                User uu = userDao.findUser(user.getAccount());
                if (uu != null) {
                    msg = 1;
                } else {
                    boolean flag = userDao.register(user);
                    if (flag) {
                        msg = 2;
                    }
                }
                hand.sendEmptyMessage(msg);
            }
        }.start();
    }

    @SuppressLint("HandlerLeak")
    final Handler hand = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    Toast.makeText(getApplicationContext(), "注册失败！", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Toast.makeText(getApplicationContext(), "该账号已经存在，请换一个账号！", Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.putExtra("a", "注册");
                    setResult(RESULT_CANCELED, intent);
                    finish();
                    break;
            }
        }
    };
}